package com.springboot.manager.aop;

import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @version v1.0
 * @ProjectName: hlk-microservice-ails
 * @ClassName: AopConfig
 * @Description: 切面日志，此种方式避免调用端依次再controller 中配置扫描项
 * @Author: zhangzl
 * @Date: 2021-08-12 8:53
 */
@Configuration
@ConditionalOnProperty(name = "log.aop.enable")
public class AopConfig {

    @Bean
    public ExceptionAop getExceptionAop() {
        return new ExceptionAop();
    }

    @Value("${log.aop.pointcut}")
    private String pointcutExecution;

    /**
     * 异常的切面
     *
     * @return
     */
    @Bean
    public DefaultPointcutAdvisor defaultPointcutAdvisor2() {
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression(pointcutExecution);
        // 配置增强类advisor
        DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
        advisor.setPointcut(pointcut);
        advisor.setAdvice(getExceptionAop());
        return advisor;
    }
}
